import {Component, OnInit, ViewChild} from '@angular/core';
import {FormControl, FormGroup} from "@angular/forms";
import {Subject, Subscription} from "rxjs";
import {TeamUser} from "../entity/team-user";
import {debounceTime} from "rxjs/operators";
import {ActivatedRoute} from "@angular/router";
import {BarnTeamService} from "../service/barn-team.service";
import {NzMessageService} from "ng-zorro-antd/message";
import {TeamUserService} from "../../services/team-user.service";

@Component({
  selector: 'app-team-user',
  templateUrl: './team-user.component.html',
  styleUrls: ['./team-user.component.less']
})
export class TeamUserComponent implements OnInit {

  teamId: string
  teamName: string


  searchForm = new FormGroup({
    'userAccount': new FormControl(),
    'userName': new FormControl(),
    'enable': new FormControl(''),
    'isInclude': new FormControl('1')
  })


  users: TeamUser[]
  current = 1;
  pageSize = 20;
  count = 1000;
  pageSizes = [this.pageSize, 50, 100]
  loading = false


  constructor(private route: ActivatedRoute, private teamService: BarnTeamService, private msg: NzMessageService,
              private teamUserService: TeamUserService) {
  }

  ngOnInit(): void {

    this.teamId = this.route.snapshot.queryParams['pid']
    this.teamName = this.route.snapshot.queryParams['teamName']
    this.search()
  }

  search() {
    const param = this.searchForm.getRawValue()
    param.teamId = this.teamId
    this.loading = true
    this.teamService.getUsers(param, this.current, this.pageSize).subscribe(
      value => {
        this.users = value.data
        this.count = value.count || 1
        this.current = value.current || 1
        this.loading = false
      }, () => this.loading = false
    )

  }

  delete(userId: string) {

    this.loading = true;
    this.teamUserService.delete(this.teamId, userId).subscribe(() => {
      this.search()
    }, () => this.loading = false)
  }

  add(userId: string) {

    this.loading = true;
    this.teamUserService.add(this.teamId, userId).subscribe(() => {
      this.search()
    }, () => this.loading = false)
  }
}
